package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.common.constants.user.UserRelationConstants;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Program: day57-leadnews
 * @Description:
 * @Data: 2022/8/5 0:26
 **/
@Service
@Slf4j
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {
        // 1. 判断用户是否登录
        ApUser user = AppThreadLocalUtils.getUser();
        Map<String, Object> map = new HashMap<>();
        if (user == null){
            // 2. 未登录 直接将 4种行为 均返回false
            // 需要返回的数据格式 : {"isfollow":true,"islike":true,"isunlike":false,"iscollection":true}
            map.put("isfollow",false);
            map.put("islike",false);
            map.put("isunlike",false);
            map.put("iscollection",false);
            return ResponseResult.okResult(map);
        } else {
            // 3. 如果登录
            // 4. 查询行为 实体
            Query query = Query.query(Criteria.where("refId").is(user.getId()).and("type").is(1));
            ApBehaviorEntry apBehaviorEntry = mongoTemplate.findOne(query, ApBehaviorEntry.class);
            // 行为实体
            String entryId = apBehaviorEntry.getId();
            // 文章id
            Long articleId = dto.getArticleId();
            // 5. 根据行为实体 文章ID查询 是否点赞
            Boolean like = isLike(entryId,articleId);
            map.put("islike",like);
            // 6. 根据行为实体 文章ID查询 是否不喜欢
            Boolean unLike = isUnlike(entryId,articleId);
            map.put("isunlike",unLike);
            // 7. 根据行为实体 文章ID查询 是否收藏
            Boolean collection = isCollection(entryId,articleId);
            map.put("iscollection",collection);
            // 8. 根据登录用户id 去redis中查询是否关注该作者
            Integer userId = user.getId();
            Integer authorId = dto.getAuthorId();
            Boolean follow = isFollow(userId,authorId);
            map.put("isfollow",follow);
            // 9. 封装结果 返回
            return ResponseResult.okResult(map);
        }
    }

    // 8. 根据登录用户id 去redis中查询是否关注该作者
    private Boolean isFollow(Integer userId, Integer authorId) {
        boolean flag = false;
        Double score = redisTemplate.opsForZSet().score(UserRelationConstants.FOLLOW_LIST + userId, String.valueOf(authorId));
        if (score != null){
            flag = true;
        }
        return flag;
    }

    // 7. 根据行为实体 文章ID查询 是否收藏
    private Boolean isCollection(String entryId, Long articleId) {
        boolean flag = false;
        Query query = Query.query(Criteria.where("entryId").is(entryId).and("articleId").is(articleId));
        ApCollection apCollection = mongoTemplate.findOne(query, ApCollection.class);
        if (apCollection != null){
            flag = true;
        }
        return flag;
    }

    // 6. 根据行为实体 文章ID查询 是否不喜欢
    private Boolean isUnlike(String entryId, Long articleId) {
        boolean flag = false;
        Query query = Query.query(Criteria.where("entryId").is(entryId).and("articleId").is(articleId));
        ApUnlikesBehavior unlikesBehavior = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
        if (unlikesBehavior != null){
            flag = true;
        }
        return flag;
    }

    // 5. 根据行为实体 文章ID查询 是否点赞
    private Boolean isLike(String entryId, Long articleId) {
        boolean flag = false;
        Query query = Query.query(Criteria.where("entryId").is(entryId).and("articleId").is(articleId));
        ApLikesBehavior likesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
        if (likesBehavior != null){
            flag = true;
        }
        return flag;
    }
}
